package cn.iocoder.yudao.module.pickbook.service.voicerecording;

import cn.iocoder.yudao.module.pickbook.dal.dataobject.voicerecording.PickbookUserVoiceRecordingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pickbook.dal.mysql.voicerecording.PickbookUserVoiceRecordingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * 用户配音记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class PickbookUserVoiceRecordingServiceImpl implements PickbookUserVoiceRecordingService {

    @Resource
    private PickbookUserVoiceRecordingMapper voiceRecordingMapper;

    @Override
    public PickbookUserVoiceRecordingDO createVoiceRecording(Long userId, Long bookId, Long chapterId, 
                                                          Long audioId, String title, String description) {
        // 创建配音记录
        PickbookUserVoiceRecordingDO recording = PickbookUserVoiceRecordingDO.builder()
                .userId(userId)
                .bookId(bookId)
                .chapterId(chapterId)
                .audioId(audioId)
                .title(title)
                .description(description)
                .score(0) // 初始评分为0
                .isShareable(true)
                .status(0)
                .build();
        
        // 保存到数据库
        voiceRecordingMapper.insert(recording);
        
        // 自动生成随机评分
        generateRandomScore(recording.getId());
        
        return recording;
    }

    @Override
    public PageResult<PickbookUserVoiceRecordingDO> getUserVoiceRecordings(Long userId, Integer pageNo, Integer pageSize) {
        // 计算偏移量
        int offset = (pageNo - 1) * pageSize;
        
        // 查询总数
        long count = voiceRecordingMapper.selectCountByUserId(userId);
        
        // 查询列表
        List<PickbookUserVoiceRecordingDO> list = voiceRecordingMapper.selectListByUserId(userId, offset, pageSize);
        
        return new PageResult<>(list, count);
    }

    @Override
    public PickbookUserVoiceRecordingDO getVoiceRecording(Long id) {
        return voiceRecordingMapper.selectById(id);
    }

    @Override
    public void updateVoiceRecordingScore(Long id, Integer score) {
        // 如果传入的评分为null，则自动生成随机评分
        if (score == null) {
            score = generateRandomScore(id);
        } else {
            // 确保评分在0-100范围内
            score = Math.max(0, Math.min(100, score));
        }
        
        // 更新评分
        PickbookUserVoiceRecordingDO recording = new PickbookUserVoiceRecordingDO();
        recording.setId(id);
        recording.setScore(score);
        voiceRecordingMapper.updateById(recording);
        
        log.info("配音记录评分已更新，ID: {}, 评分: {}", id, score);
    }

    @Override
    public void deleteVoiceRecording(Long id) {
        voiceRecordingMapper.deleteById(id);
    }

    /**
     * 自动生成随机评分
     * 
     * @param recordingId 配音记录ID
     * @return 生成的随机评分
     */
    private Integer generateRandomScore(Long recordingId) {
        Random random = new Random();
        // 生成60-100之间的随机评分，偏向高分
        int score = 60 + random.nextInt(41); // 60-100
        
        // 更新数据库中的评分
        PickbookUserVoiceRecordingDO recording = new PickbookUserVoiceRecordingDO();
        recording.setId(recordingId);
        recording.setScore(score);
        voiceRecordingMapper.updateById(recording);
        
        log.info("配音记录自动评分已生成，ID: {}, 评分: {}", recordingId, score);
        return score;
    }
} 